﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Sunboxsoft.WinForm;
using Sunboxsoft.Common;
using Service = Sunboxsoft.ASMS.Client.Domain.Service;
using WebRef = Sunboxsoft.ASMS.Client.Domain.WebRef;
using Sunboxsoft.Common;
using DevExpress.XtraEditors;

namespace Sunboxsoft.ASMS.Client.UI.Management
{
    public partial class ServiceItemListView : UserControl, IView
    {
        private int _pageIndex;
        private int _pageCount;

        public ServiceItemListView()
        {
            InitializeComponent();
        }

        #region override
        protected override void OnLoad(EventArgs e)
        {
            LoadServiceCategoryListData();
            LoadServiceItemListData();
            base.OnLoad(e);
        }
        #endregion


        #region IView 成员

        public void RegisterAction(ActionNodeCollection collection)
        {
            collection.AddAction("syncFromGlobal", "同步", OnSyncActionClick)
                .AddAction("refresh", "刷新", OnRefreshActionClick);
        }
        public void Resume(ViewResumeArgs args)
        {
        }
        public void Active(ViewActiveArgs args)
        {

        }
        public ViewHeader GetHeader()
        {
            return new ViewHeader("");
        }
        #endregion

        #region methods
        private void LoadServiceCategoryListData()
        {
            this.ShowMask("Loading...");
            UIContext.Current.InvokeAsync(() =>
            {
                PagedResult<WebRef.EServiceCategory> result = new Service.QueryService().QueryServiceCategoryPagedList(ModuleContext.Current.BusinessId,
                    ModuleContext.Current.StationId, Domain.Constant.CommonStatus.ALL, 0, 9999);
                this.BeginInvoke(new Action<PagedResult<WebRef.EServiceCategory>>((p) =>
                {
                    if (p.IsSuccess)
                    {
                        this.ctlLookupCategory.Properties.DisplayMember = "Name";
                        this.ctlLookupCategory.Properties.ValueMember = "Id";
                        this.ctlLookupCategory.Properties.DataSource = p.Data.Items;
                    }
                    else
                    {
                        UIContext.Current.ViewManager.ShowError(p.Message);
                    }
                    this.HideMask();
                }), result);
            });
        }

        private void LoadServiceItemListData()
        {
            this.ShowMask("Loading...");
            UIContext.Current.InvokeAsync(() =>
            {
                PagedResult<WebRef.VServiceItem> result = new Service.QueryService().QueryServiceItemPagedList(
                    this.ctlEditQry.Text.Trim(),
                    GetSelectedCategoryId(),
                    ModuleContext.Current.BusinessId,
                    ModuleContext.Current.StationId,
                    Domain.Constant.CommonStatus.Enabled,
                    _pageIndex * GetPageLimit(), GetPageLimit());
                this.BeginInvoke(new Action<PagedResult<WebRef.EServiceItem>>((p) =>
                {
                    if (p.IsSuccess)
                    {
                        ctlGrid.Paginate<WebRef.VServiceItem>(result, out _pageIndex, out _pageCount);
                    }
                    else
                    {
                        UIContext.Current.ViewManager.ShowError(p.Message);
                    }
                    this.HideMask();
                }), result);
            });
        }

        private Guid? GetSelectedCategoryId()
        {
            WebRef.EServiceCategory category = ctlLookupCategory.GetSelectedDataRow() as WebRef.EServiceCategory;
            if (category != null)
            {
                return category.Id;
            }
            return null;
        }

        private int GetPageLimit()
        {
            return 25;
        }
        #endregion

        #region actions

        private void OnSyncActionClick(object sender, object e)
        {
            if (UIContext.Current.ViewManager.ShowQuestion("是否从全局数据中心同步服务项目？") == false)
            {
                return;
            }

            this.ShowMask("Syncing...");
            UIContext.Current.InvokeAsync(() =>
            {
                //ServiceResult result = new Service.SyncService().SyncServiceItemFromGlobal(ModuleContext.Current.BusinessId.Value, ModuleContext.Current.StationId);
                //this.BeginInvoke(new Action<ServiceResult>((p) =>
                //{
                //    if (p.IsSuccess)
                //    {
                //        LoadServiceItemListData();
                //    }
                //    else
                //    {
                //        UIContext.Current.ViewManager.ShowError(p.Message);
                //    }
                //    this.HideMask();
                //}), result);
            });
        }

        private void OnRefreshActionClick(object sender, object e)
        {
            LoadServiceItemListData();
        }

        private void ctlGrid_EmbeddedNavigator_ButtonClick(object sender, NavigatorButtonClickEventArgs e)
        {
            NavigatorCustomButton button = e.Button as NavigatorCustomButton;
            if (button == null)
            {
                return;
            }
            if ((NavigatorButtonType)button.Tag == NavigatorButtonType.First)
            {
                _pageIndex = 0;
                LoadServiceItemListData();
            }
            else if ((NavigatorButtonType)button.Tag == NavigatorButtonType.PrevPage)
            {
                _pageIndex--;
                LoadServiceItemListData();
            }
            else if ((NavigatorButtonType)button.Tag == NavigatorButtonType.NextPage)
            {
                _pageIndex++;
                LoadServiceItemListData();
            }
            else if ((NavigatorButtonType)button.Tag == NavigatorButtonType.First)
            {
                _pageIndex = _pageCount - 1;
                LoadServiceItemListData();
            }
        }

        private void ctlLookupCategory_Properties_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            string tagString = Convert.ToString(e.Button.Tag);
            if (string.Equals(tagString, "clear"))
            {
                ctlLookupCategory.EditValue = null;
            }
        }
        #endregion
    }
}
